package com.ivr.cm.handler.validate.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.AbstractMap;
import java.util.Date;

import org.apache.log4j.Logger;

import com.ivr.cm.cache.Cacheable;
import com.ivr.cm.data.ValidateData;
import com.ivr.cm.handler.validate.ValidateServiceIF;
import com.ivr.cm.service.StateCode;
import com.ivr.cm.vo.ConfigBusinessVo;

/**
 * 判断业务是否开通
 * @author liugeng
 * @date 2015-12-15
 *
 */
public class ValidateConfigBusinessImpl implements ValidateServiceIF {
	private static Logger logger = Logger.getLogger(ValidateConfigBusinessImpl.class);
	
	private Cacheable configBusinessCache;

	@SuppressWarnings("unchecked")
	@Override
	public int doProcess(ValidateData validateData) {
		/**获得主叫号码*/
		String mobile = validateData.getBaseData().getCalling();
		/**获得业务号码短号码*/
		String sp_code = validateData.getBaseData().getShort_called();
		logger.info(String.format("[%s][%s][业务检查鉴权]开始", mobile, sp_code));
		
		/**从缓存中获得SP号码的业务配置信息*/
		AbstractMap<String, ConfigBusinessVo> configBusinessMap = (AbstractMap<String, ConfigBusinessVo>) configBusinessCache.getCache();
		ConfigBusinessVo configBusinessVo = configBusinessMap.get(sp_code);
		
		if(configBusinessVo != null && configBusinessVo.getStatus() == 1){
			logger.info(String.format("[%s][%s][业务检查鉴权]业务[开通]", mobile, sp_code));
			String start_time = configBusinessVo.getStart_time();
			String end_time = configBusinessVo.getEnd_time();
			if(start_time != null && !"".equals(start_time) && end_time != null && !"".equals(end_time)){
				logger.info(String.format("[%s][%s][业务检查鉴权]业务[设置屏蔽时间]", mobile, sp_code));
				boolean isIntime = isInTime(start_time, end_time);
				if(isIntime){
					logger.info(String.format("[%s][%s][业务检查鉴权]业务[设置屏蔽时间][屏蔽时间内]",  mobile, sp_code));
					validateData.setResult(ValidateData.RESULT_INVALID_OTHER);
					validateData.setVoice(ValidateData.VOICE_INVALID_OTHER);
					validateData.setTimeRemain(0);
					validateData.setStatecode(StateCode.SPNUMBER_FORBIDDEN);
					return ValidateChain.VALIDATE_FINISHED;
				}
				logger.info(String.format("[%s][%s][业务检查鉴权]业务[设置屏蔽时间][屏蔽时间外]",  mobile, sp_code));
				return ValidateChain.VALIDATE_CONTINUE;
			}
			logger.info(String.format("[%s][%s][业务检查鉴权]业务[未设置屏蔽时间]", mobile, sp_code));
			return ValidateChain.VALIDATE_CONTINUE;
		}else{
			logger.info(String.format("[%s][%s][业务检查鉴权]业务[关停]", mobile, sp_code));
			
			validateData.setResult(ValidateData.RESULT_INVALID_OTHER);
			validateData.setVoice(ValidateData.VOICE_INVALID_OTHER);
			validateData.setTimeRemain(0);
			validateData.setStatecode(StateCode.SPNUMBER_OFF);
			return ValidateChain.VALIDATE_FINISHED;
		}
	}
	
	private static boolean isInTime(String start_time, String end_time){
		Date date = new Date();
		DateFormat df = new SimpleDateFormat("HH:mm:ss");
		String current = df.format(date);
		start_time = start_time + ":00";
		end_time = end_time + ":59";
		if (start_time.compareTo(end_time) < 0) { 
			// 开始时间早于结束时间
			return (current.compareTo(start_time) >= 0 && current.compareTo(end_time) <= 0);
		} else { 
			// 开始时间晚于结束时间（凌晨）
			return (current.compareTo(start_time) >=0 || current.compareTo(end_time) <= 0);
		}
	}

	public Cacheable getConfigBusinessCache() {
		return configBusinessCache;
	}
	public void setConfigBusinessCache(Cacheable configBusinessCache) {
		this.configBusinessCache = configBusinessCache;
	}
}